package com.latvia.ui;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.latvia.cookingapp.CookingApp;
import com.latvia.cookingapp.R;
import com.latvia.managers.AppManager;
import com.latvia.managers.DataManager;
import com.latvia.managers.WebserviceManager;
import com.latvia.model.Recipe;
import com.latvia.utils.Constants;
import com.latvia.utils.Enums;
import com.latvia.utils.JsonQueryAsyncTask;

public class MainActivity extends FragmentActivity implements JsonQueryAsyncTask.OnTaskCompletedListener {
	public static final String TAG = MainActivity.class.getName();
	private SlidingMenu slidingMenu;
	private Fragment contentView;
	private Fragment currentFragment;
	
	private static WebserviceManager instance;
	private JsonQueryAsyncTask task;
	private int parseMode;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		slidingMenu = new SlidingMenu(this);
		slidingMenu.setMode(SlidingMenu.LEFT);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		slidingMenu.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
		// slidingMenu.setShadowDrawable(R.drawable.slidingmenu_shadow);
		 slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		//slidingMenu.setBehindWidth(350);
		slidingMenu.setFadeDegree(0.2f);
		slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		slidingMenu.setMenu(R.layout.sliding_menu);

		if (savedInstanceState != null)
			contentView = getSupportFragmentManager().getFragment(
					savedInstanceState, "contentView");
		if (contentView == null) {
			executeGetAllRecipes();
			contentView = new AllCategoriesFragment();
		}

//		getSupportFragmentManager().beginTransaction()
//				.replace(R.id.content_view, contentView).commit();
		
		

		ListView listView = (ListView) findViewById(R.id.list);
		MenuListViewAdapter menuAdapter = new MenuListViewAdapter(this);
		listView.setAdapter(menuAdapter);

		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View itemClicked,
					int position, long id) {

				Toast.makeText(
						getApplicationContext(),
						"Selected: "
								+ AppManager.getInstance().getMenuItems()
										.get(position).toString(),
						Toast.LENGTH_SHORT).show();

				Enums.MenuItem item = Enums.MenuItem
						.getMenuItemByPosition(position);
				switch_content(item);

			}
		});
		
		
//		WebserviceManager webserviceManager = WebserviceManager.getInstance();
//		webserviceManager.executeGetAllRecipes();

		// getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	private void switch_content(Enums.MenuItem item) {
		Fragment fragment = null;
		switch (item) {

		case ALL_CATEGORIES:
			executeGetAllRecipes();
			fragment = new AllCategoriesFragment();
			break;
		case FAVOURITE:
			fragment = new FavouritesFragment();
			break;
		case FRIENDS:
		case MY_COOKING:
		case COOK_LATER:
		case ABOUT:
			fragment = new AboutFragment();
			break;
		case QUIT:
			fragment = new AllCategoriesFragment();
		}

		if (fragment != null) {
			AppManager.getInstance().setSelectedMenuItem(item);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_view, fragment).commit();
			contentView = fragment;
			slidingMenu.showContent();
		}
	}
	
	public void executeGetAllRecipes() {
		String query = String.format("%s/%s%s", Constants.BASE_URL, Constants.RECIPES, Constants.JSON); 
		
		task = new JsonQueryAsyncTask(this, query, Constants.recipes);
		task.setOnTaskCompletedListener(this);
		task.execute();
	}
	
	public void executeFavouriteRecipes() {
		//http://localhost:8080/cooking/rest/users/1/favourites?format=xml
		String query = String.format("%s/%s/%s/%s/%s", Constants.BASE_URL, Constants.USERS, AppManager.getInstance().getUserId(), Constants.FAVOURITES,Constants.JSON); 
		
		task = new JsonQueryAsyncTask(this, query, Constants.favourites);
		task.setOnTaskCompletedListener(this);
		task.execute();
	}


	@Override
	public void onQueryTaskCompleted(ArrayList<Recipe> result) {
		DataManager.getInstance().setRecipes(result);
		AllCategoriesFragment allCatFragment = new AllCategoriesFragment();
		
		
		contentView =  new AllCategoriesFragment();
		//allCatFragment.reload(result);
		
		getSupportFragmentManager().beginTransaction()
		.replace(R.id.content_view, contentView).commit();
		
		
//		Enums.MenuItem item = AppManager.getInstance().getSelectedMenuItem();
//		Fragment fragment = null;
//		switch (item) {
//
//		case ALL_CATEGORIES:
//			
//			fragment = new AllCategoriesFragment();
//			AllCategoriesFragment allCatFragment = (AllCategoriesFragment)fragment;
//			allCatFragment.reload(result);
//			
//			break;
//		case FAVOURITE:
//			fragment = new FavouritesFragment();
//			break;
//		case FRIENDS:
//		case MY_COOKING:
//		case COOK_LATER:
//		case ABOUT:
//			fragment = new AboutFragment();
//			break;
//		case QUIT:
//			fragment = new AllCategoriesFragment();
//		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		if (slidingMenu.isMenuShowing()) {
			slidingMenu.toggle();
		} else {
			super.onBackPressed();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			this.slidingMenu.toggle();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.slidingMenu.toggle();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
